/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * TrainingBook
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * TrainingBook is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TrainingBook is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <Application.hpp>
#include <config.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <filesystem-utils.hpp>
#include <stdlib.h>


///@brief Default constructor
Application::Application()
{
    name = "Training Book";
    version = TRAININGBOOK_VERSION;
}

///@brief Destructor
Application::~Application()
{
    
}

///@brief Read configuration from a file
///@param filepath Path to the configuration file to read
bool Application::ReadConfigurationFile(std::string filepath)
{
    if(!FileExists(filepath))
    {
        std::cout << "Le fichier " << filepath << " n'existe pas. Un fichier de configuration par défaut va être créé\n";
        ///@todo Write a routine to create a default config.txt file
        CreateDefaultConfigFile(filepath);
        //return false;
    }
    std::ifstream fin(filepath.c_str());
    unsigned int i = 0;
    while(!fin.eof())
    {
        fin >> paramNames[i] >> paramTypes[i] >> paramValues[i];
        i++;
    }
    return true;
}

///@brief Return value of a given parameters
///@param paramName Name of the parameter to return
std::string Application::GetParameter(std::string paramName)
{
    for(unsigned int i = 0 ; i < PARAMETERS_NUMBER ; i++)
    {
        if (paramNames[i] == paramName)
            return paramValues[i];
    }
    std::cerr << "Parameter " << paramName << " not defined\n";
    return "";
}

///@brief Return value of a given parameter with integer value
///@param paramName Name of the parameter to return
int Application::GetIntegerParameter(std::string paramName)
{
    for(unsigned int i = 0 ; i < PARAMETERS_NUMBER ; i++)
    {
        if((paramNames[i] == paramName)&&(paramTypes[i] == "int"))
        {
            std::istringstream stream(paramNames[i]);
            int res;
            stream >> res;
            if(stream.fail())
                std::cerr << "Error in Application::GetIntegerParameter()\n";
            return res;
        }
    }
    std::cerr << "Parameter " << paramName << " not defined\n";
    return 0;
}

///@brief Print actual config
void Application::PrintConfig()
{
    for (unsigned int i = 0 ; i < PARAMETERS_NUMBER ; i++)
        std::cout << paramNames[i] << "\t" << paramTypes[i] << "\t" << paramValues[i] << "\n";
}

///@brief Create a default configuration file
///@param filePath Path to the file to create
void Application::CreateDefaultConfigFile(std::string filePath)
{
    std::ofstream fout(filePath.c_str());
    if(TRAININGBOOK_OS == "Windows")
    {
        fout << "TILES_CACHE\tstring\t" << getenv(TRAININGBOOK_USER_DIR_DRIVE_ENV_VAR) << getenv(TRAININGBOOK_USER_DIR_ENV_VAR) << TRAININGBOOK_FILE_SEPARATOR << ".trainingbook" << TRAININGBOOK_FILE_SEPARATOR << "cache" << "\n";
        fout << "TRAININGBOOK_DIR\tstring\t" << getenv(TRAININGBOOK_USER_DIR_DRIVE_ENV_VAR) << getenv(TRAININGBOOK_USER_DIR_ENV_VAR) << TRAININGBOOK_FILE_SEPARATOR << "TrainingBook" << "\n";
    }
    else
    {
        
        fout << "TILES_CACHE\tstring\t" << getenv(TRAININGBOOK_USER_DIR_ENV_VAR) << TRAININGBOOK_FILE_SEPARATOR  << ".trainingbook" << TRAININGBOOK_FILE_SEPARATOR << "cache" << "\n";
        fout << "TRAININGBOOK_DIR\tstring\t" << getenv(TRAININGBOOK_USER_DIR_ENV_VAR) << TRAININGBOOK_FILE_SEPARATOR  << "TrainingBook" << "\n";
    }
    fout << "TRAININGBOOK_DEFAULT_FILE_NAME\tstring\tTrainingBook.xml\n";
    fout << "TILES_REPOSITORY\tstring\thttp://tile.openstreetmap.org/\n";

    
    
    fout.close();
}

